package com.module_live.view;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.luck.picture.lib.tools.ToastManage;
import com.module_live.R;
import com.module_live.activity.CoachStudyLivingActivity;
import com.module_live.bean.coachstudy.StudentListDataBean;
import com.module_live.bean.coachstudy.StudentZanAndFlowerBean;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class StudentListAndGiftPopup {

    private Activity activity;

    public View parentView;
    StudentListAdapter listAdapter;

    List<StudentListDataBean.ObjBean> studentDataList = new ArrayList<>();

    public StudentListAndGiftPopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();
    }

    private EasyPopup popu;


    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.popup_study_studentgift)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);

                    }

                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    public RecyclerView rvList;

    private void initView(View view) {

        rvList = view.findViewById(R.id.rv_list);

        listAdapter = new StudentListAdapter(activity, studentDataList);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(activity);
        rvList.setLayoutManager(manager);
        rvList.setAdapter(listAdapter);

    }

    public void updateStudentListData() {
        initStudentListData();
    }

    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        popu.showAtLocation(view, Gravity.CENTER, 0, 0);

    }


    class StudentListAdapter extends BaseAdapter<StudentListDataBean.ObjBean> {

        public StudentListAdapter(Context context, List<StudentListDataBean.ObjBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_study_gift;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<StudentListDataBean.ObjBean> mData) {

            final StudentListDataBean.ObjBean objBean = mData.get(position);

            ImageView ivUser = holder.getView(R.id.iv_user);
            TextView tvName = holder.getView(R.id.tv_name);

            LinearLayout linZan = holder.getView(R.id.lin_zan);
            final ImageView ivZan = holder.getView(R.id.iv_zan);
            final TextView tvZan = holder.getView(R.id.tv_zan);

            LinearLayout linFlower = holder.getView(R.id.lin_flower);
            final ImageView ivFlower = holder.getView(R.id.iv_flower);
            final TextView tvFlower = holder.getView(R.id.tv_flower);

            if (StringUtil.isNotEmpty(objBean.getPhoto_url(), true)) {
                Glide.with(activity)
                        .load(objBean.getPhoto_url())
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivUser);
            }

            if (StringUtil.isNotEmpty(objBean.getUser_name(), true)) {
                tvName.setText(objBean.getUser_name());
            }

            //初始化点赞和鲜花的状态
            if (objBean.getLike_total() > -1) {
                if (0 == objBean.getLike_total()) {

                } else {
                    ivZan.setBackgroundResource(R.mipmap.icon_give);
                }
                tvZan.setText(objBean.getLike_total() + "");
            }

            if (objBean.getRed_flower() > -1) {
                if (0 == objBean.getRed_flower()) {

                } else {
                    ivFlower.setBackgroundResource(R.mipmap.icon_flower);
                }
                tvFlower.setText(objBean.getRed_flower() + "");
            }

            //点赞、鲜花点击事件
            linZan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if ("1".equals(SharedPreferenceManager.getInstance(activity).getUserCache().getUserIdentity())) {
                        sendZanAndFlowerToService(objBean.getUser_id(), 0, tvZan, ivZan, tvFlower, ivFlower);
                    } else {
                        ToastManage.s(activity, "你不能给同学点赞哦！");
                    }
                }
            });

            linFlower.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if ("1".equals(SharedPreferenceManager.getInstance(activity).getUserCache().getUserIdentity())) {
                        sendZanAndFlowerToService(objBean.getUser_id(), 1, tvZan, ivZan, tvFlower, ivFlower);
                    } else {
                        ToastManage.s(activity, "你不能给同学送花哦！");
                    }
                }
            });

        }
    }

    private void sendZanAndFlowerToService(int studentId, final int type, final TextView tvZan,
                                           final ImageView ivZan, final TextView tvFlower, final ImageView ivFlower) {

        HttpUtils.with(activity)
                .addParam("requestType", "LIKE_AND_FLOWER")
                .addParam("token", SharedPreferenceManager.getInstance(activity).getUserCache().getUserToken())
                .addParam("student_id", studentId)
                .addParam("course_id", CoachStudyLivingActivity.course_id)
                .addParam("like_or_flower", type)
                .execute(new HttpNormalCallBack<StudentZanAndFlowerBean>() {
                    @Override
                    public void onSuccess(StudentZanAndFlowerBean result) {
                        if (result.getCode() > 0) {
                            if (CodeUtil.CODE_200 == result.getCode()) {

                                if (0 == type) {
                                    ivZan.setBackgroundResource(R.mipmap.icon_give);
                                    ToastManage.s(activity, "点赞成功！");
                                } else if (1 == type) {
                                    ivFlower.setBackgroundResource(R.mipmap.icon_flower);
                                    ToastManage.s(activity, "送花成功！");
                                }
                                tvZan.setText(result.getObj().getLike_total() + "");
                                tvFlower.setText(result.getObj().getRed_flower() + "");

                            } else if (478 == result.getCode()) {//送小红花已有9次，请勿重复送小红花

                                ToastManage.s(activity, "每堂课最多送9次花哦！");
                            } else if (477 == result.getCode()) {//点赞已有9次，请勿重复点赞

                                ToastManage.s(activity, "每堂课最多点赞9次哦");
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });

    }

    public void initStudentListData() {

        HttpUtils.with(activity)
                .addParam("requestType", "LIVE_STUDIO_STUDENTS")
                .addParam("token", SharedPreferenceManager.getInstance(activity).getUserCache().getUserToken())
                .addParam("course_id", CoachStudyLivingActivity.course_id)
                .execute(new HttpNormalCallBack<StudentListDataBean>() {
                    @Override
                    public void onSuccess(StudentListDataBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            if (result.getObj() != null && result.getObj().size() > 0) {

                                if (studentDataList.size() > 0) {
                                    studentDataList.clear();
                                }
                                studentDataList.addAll(result.getObj());
                                listAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });

    }

}
